﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Text.RegularExpressions;

namespace MapCenterSystem.Utility
{
    public class StringHelper
    {
        public static List<string> GetNumberAndStreet(string keyword) // keyword phai khac empty
        {
            List<string> result = new List<string>();

            if (char.IsNumber(keyword[0]))
            {
                int i = keyword.IndexOf(' ');
                if (i == -1)
                {
                    result.Add("");
                    result.Add(keyword);
                }
                else
                {
                    result.Add(keyword.Substring(0, i));
                    result.Add(keyword.Substring(i + 1));
                }
                
            }
            else
            {
                //result.Add("");   //không có số nhà thì luôn mặt định là 1
                result.Add("1");
                result.Add(keyword);
            }
            
            return result;
        }

        public static string GetExactNumber(string number)
        {
            int pos = 0;
            while (pos < number.Length)
            {
                if (char.IsNumber(number[pos]))
                    pos += 1;
                else
                    break;
            }

            return number.Substring(0, pos);
        }

        public static string RemoveMultiSpace(string s)
        {
            string result = string.Empty;
            char[] arr = new char[] {' ' };
            string[] arrStr = s.Split(arr, StringSplitOptions.RemoveEmptyEntries);

            result += arrStr[0];
            for(int i = 1; i < arrStr.Length; i++)
                result += " " + arrStr[i];

            return result;            
        }

        public static string RemoveStrangeCharacter(string s)
        {
            string result = s;
            string strTemp = ConvertToASCII(s);
            List<char> lstCharacter = new List<char>();
            Regex reg = new Regex(@"[a-zA-Z0-9/\s\.\-]");
            for (int i = 0; i < result.Length; i++)
                if (!reg.IsMatch(strTemp[i].ToString()))
                    lstCharacter.Add(result[i]);

            foreach (char temp in lstCharacter)
                result = result.Replace(temp, ' ');
            return result;
        }

        public static string StandardizeString(string s)
        {
            string result = RemoveStrangeCharacter(s);
            result = RemoveMultiSpace(result);
            return result;
        }

        public static string GetSearchString(string s, string pathfile)
        {
            XMLHelper.LoadFile(pathfile);
            string word = ConvertToASCII(s);
            string result = word;
            List<string> lstAbbr = XMLHelper.GetListAbbr();
            for (int i = 0; i < lstAbbr.Count; i++ )
            {
                int t = word.IndexOf(lstAbbr[i].ToUpper());
                if (t != -1 && (((t != 0 && word[t - 1] == ' ') || t == 0) && ((t + lstAbbr[i].Length != word.Length && word[t + lstAbbr[i].Length] == ' ') || t + lstAbbr[i].Length == word.Length)))
                {
                    List<string> lstExpansion = XMLHelper.GetListPat(lstAbbr[i]);
                    string strReplace = "(" + "\"" + lstExpansion[0] + "\"";
                    for (int j = 1; j < lstExpansion.Count; j++)
                        strReplace += " OR " + "\"" + lstExpansion[j] + "\"";
                    strReplace += ")";
                    strReplace = strReplace.Replace(" ", "?"); // danh dau
                    result = result.Replace(lstAbbr[i].ToUpper(), strReplace);
                }
            }

            result = "\"" + result + "\"";
            result = result.Replace(" ", "\" AND \"");
            result = result.Replace("?", " ");
            result = result.Replace(")\"", ")");
            result = result.Replace("\"(", "(");

            return result;
        }

        public static string ConvertToASCII(string text)
        {
            string s = text;
            string[] VietnameseSigns = new string[]
            {
                "aAeEoOuUiIdDyY",
                "áàạảãâấầậẩẫăắằặẳẵ",
                "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
                "éèẹẻẽêếềệểễ",
                "ÉÈẸẺẼÊẾỀỆỂỄ",
                "óòọỏõôốồộổỗơớờợởỡ",
                "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
                "úùụủũưứừựửữ",
                "ÚÙỤỦŨƯỨỪỰỬỮ",
                "íìịỉĩ",
                "ÍÌỊỈĨ",
                "đ",
                "ĐÐ",
                "ýỳỵỷỹ",
                "ÝỲỴỶỸ" };


            for (int i = 1; i < VietnameseSigns.Length; i++)
                for (int j = 0; j < VietnameseSigns[i].Length; j++)
                    s = s.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
            return s.ToUpper();
        }
    }
}
